package com.zmn.plat.admin.controller.service.group;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.service.group.ServiceGroup;
import com.zmn.plat.model.vo.service.group.ServiceGroupCopyVO;

/**
 * 故障分组表
 *
 * @author tanbiao
 * @since 2019-11-21 18:57:24
 */
@RestController
@RequestMapping("/service/group/")
@SuppressWarnings("Duplicates")
public class ServiceGroupModifyController extends ServiceGroupBaseController {

    private static final long serialVersionUID = -5983717525550895208L;

    /**
     * 新增修改
     *
     * @param faultGroup
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/service/group/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ServiceGroup faultGroup) {
        try {
            faultGroupBService.addEdit(faultGroup, getMcStaffRealName());
            return ResponseDTO.success(Integer.toString(faultGroup.getGroupId()), "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("不能重复添加！", faultGroup.getGroupName());
            return ResponseDTO.fail("数据已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error("新增/修改故障分组表失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 新增修改
     *
     * @param faultGroup
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "copyGroupData")
    @RequiresPermissions("/service/group/copyGroup.action")
    public ResponseDTO copyGroupData(HttpServletRequest request, @RequestBody ServiceGroupCopyVO faultGroup) {
        try {
            faultGroupBService.copyGroupData(faultGroup, getMcStaffRealName());
            return ResponseDTO.success(Integer.toString(faultGroup.getTariffId()), "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("不能重复添加！", faultGroup.getTariffId());
            return ResponseDTO.fail("请先修改当前已复制的项目分组的别称，再进行复制");
        } catch (Exception ex) {
            logger.error("新增/修改故障分组表失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/service/group/detail.action")
    public ResponseDTO updateSort(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        Integer result = faultGroupService.updateSortByKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/service/group/detail.action")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (status == 0) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);

        Integer result = faultGroupService.updateStatusBatchByKey(arrayIds, status, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }

    /**
     * 删除分组
     *
     * @param groupId
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "deleteGroup")
    @RequiresPermissions("/service/group/deleteGroup.action")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "groupId", defaultValue = "0") Integer groupId) {

        Integer result = faultGroupService.deleteByKey(groupId);
        return ResponseDTO.success(result, "操作成功！");
    }

}
